package com.baozhun.mall.common.widget.customview.colorselect

import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.RelativeLayout
import com.baozhun.mall.common.R
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.zhpan.bannerview.utils.BannerUtils
import de.hdodenhof.circleimageview.CircleImageView
import me.hgj.jetpackmvvm.base.appContext

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/13
 * @Version：1.0.0
 */
class ColorButton : RelativeLayout {
    var mImageView: ImageView? = null

    constructor(context: Context?) : super(context!!) {
        initView(context)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
        initView(context)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
        context!!,
        attrs,
        defStyle
    ) {
        initView(context)
    }

    fun initView(context: Context) {
        val root = LayoutInflater.from(context).inflate(R.layout.widget_color_button, this)
        mImageView = root.findViewById(R.id.color_iv)
    }

    fun setColor(colorId: Int) {
        var bitmap = Bitmap.createBitmap(BannerUtils.dp2px(12f), BannerUtils.dp2px(12f), Bitmap.Config.ARGB_8888)
        bitmap.eraseColor(colorId)
        mImageView?.setImageBitmap(bitmap)
    }

    fun setColor(url: String, colorId: Int) {
        if (url.isNotEmpty()) {
            Glide.with(appContext)
                .asBitmap()
                .apply(RequestOptions().optionalTransform(RoundedCorners(SizeUtils.dp2px(2f))))
                .load(url).into(mImageView!!)
        } else {
            setColor(colorId)
        }
    }
}